/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ActivityFram.java
 *
 * Created on 13-12-2011, 20:40:23
 */
package view;

import controller.ActivityController;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import model.Activity;
import view.table.model.ActivityTableModel;

/**
 *
 * @author Martin Mohr Pedersen
 */
public class ActivityView extends javax.swing.JFrame {
    
    /**
     * Listener class for activity tables
     */
    class ActivityListSelectionListener implements ListSelectionListener {
            
        JTable table;

        public ActivityListSelectionListener(JTable table) {
            this.table = table;
        }
        
        @Override
        public void valueChanged(ListSelectionEvent e) {
            int index = table.getSelectedRow();
            
            if(index == -1) {
                return; // Not a valid selection
            }
            
            ActivityTableModel model = (ActivityTableModel) table.getModel();
            
            Activity activity = model.getActivityFromIndex(index);

            showActivity(activity);
            clearSelections(table);
            
        }
    }
    
    private Activity selectedActivity;
    
    // tables
    private ActivityTableModel tblModelFri;
    private ActivityTableModel tblModelSat;
    private ActivityTableModel tblModelSun;
    
    /** Creates new form ActivityFram */
    public ActivityView() {
        
        setUndecorated(true);
        initComponents();
        setExtendedState(MAXIMIZED_BOTH);
        
        initTables();
        
        // populate tables
        Activity[] activities = new ActivityController().getActivitiesForCurrentEvent();
        populateTables(activities);
        
        // hide details panel
        pnlDetails.setVisible(false);
    }
    
    /**
     * Clears selection from all tables except the selected table
     * @param selectedTable 
     */
    public void clearSelections(JTable selectedTable) {
        if(selectedTable != tblFri) {
            tblFri.clearSelection();
        }
        
        if(selectedTable != tblSat) {
            tblSat.clearSelection();
        }
        
        if(selectedTable != tblSun) {
            tblSun.clearSelection();
        }
    }
    
    /**
     * Initializes activity tables with proper table models and selection listeners
     */
    private void initTables() {
        
        tblModelFri = new ActivityTableModel();
        tblFri.setModel(tblModelFri);
        tblFri.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        tblFri.getSelectionModel().addListSelectionListener(new ActivityListSelectionListener(tblFri));
        tblFri.getTableHeader().setReorderingAllowed(false);
        
        tblModelSat = new ActivityTableModel();
        tblSat.setModel(tblModelSat);
        tblSat.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        tblSat.getSelectionModel().addListSelectionListener(new ActivityListSelectionListener(tblSat));
        tblSat.getTableHeader().setReorderingAllowed(false);
        
        tblModelSun = new ActivityTableModel();
        tblSun.setModel(tblModelSun);
        tblSun.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        tblSun.getSelectionModel().addListSelectionListener(new ActivityListSelectionListener(tblSun));
        tblSun.getTableHeader().setReorderingAllowed(false);
        
    }
    
    /**
     * Displays activity details in the frame
     * @param activity 
     */
    public void showActivity(Activity activity) {
        
        if(activity == null) {
            return;
        }
        
        pnlDetails.setVisible(true);
        
        selectedActivity = activity;
        
        lblActivityName.setText(activity.getName());
        txtActivityDescription.setText(activity.getDescription());
        
        SimpleDateFormat sdf = new SimpleDateFormat("EEEE 'kl.' HH:mm");
        
        String time = sdf.format(activity.getTimeOfStart());
        String place = activity.getRoom().getName();
        
        String timePlace = time + " - " + place;
        
        lblActivityTimePlace.setText(timePlace);
        
        boolean requiresSignup = activity.getRequiresSignup();
        
        if(requiresSignup) {
            
            btnStartSignup.setVisible(true);
            lblActivityRequiresSignup.setVisible(true);
            
            if(Calendar.getInstance().after(activity.getTimeOfStart())) {
                lblActivityRequiresSignup.setText("Aktiviteten er allerede blevet afholdt.");
                btnStartSignup.setVisible(false);
            }
            
        } else {
            lblActivityRequiresSignup.setVisible(false);
            btnStartSignup.setVisible(false);
        }
        
    }
    
    /**
     * Populates the activity tables with the given activities
     * @param activities 
     */
    private void populateTables(Activity[] activities) {
        Calendar cal = Calendar.getInstance();
        
        for(Activity activity : activities) {
            Date start = activity.getTimeOfStart();
            cal.setTime(start);
            int day = cal.get(Calendar.DAY_OF_WEEK);
            
            ActivityTableModel model;
            
            switch(day) {
                case Calendar.FRIDAY:
                    model = tblModelFri;
                    break;
                    
                case Calendar.SATURDAY:
                    model = tblModelSat;
                    break;
                    
                case Calendar.SUNDAY:
                    model = tblModelSun;
                    break;
                    
                default: 
                    System.out.println("No valid table for activity: " + activity.getName());
                    continue;
            }
            
            model.addActivity(activity);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlActivitySchedule = new javax.swing.JPanel();
        pnlFri = new javax.swing.JPanel();
        jScrollPane6 = new javax.swing.JScrollPane();
        tblFri = new javax.swing.JTable();
        jLabel7 = new javax.swing.JLabel();
        pnlSat = new javax.swing.JPanel();
        jScrollPane7 = new javax.swing.JScrollPane();
        tblSat = new javax.swing.JTable();
        jLabel8 = new javax.swing.JLabel();
        pnlSun = new javax.swing.JPanel();
        jScrollPane8 = new javax.swing.JScrollPane();
        tblSun = new javax.swing.JTable();
        jLabel9 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        pnlDetails = new javax.swing.JPanel();
        lblActivityName = new javax.swing.JLabel();
        lblActivityTimePlace = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtActivityDescription = new javax.swing.JTextPane();
        lblActivityRequiresSignup = new javax.swing.JLabel();
        btnStartSignup = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tblFri.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblFri.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblFri.setShowVerticalLines(false);
        jScrollPane6.setViewportView(tblFri);

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel7.setText("Fredag");

        javax.swing.GroupLayout pnlFriLayout = new javax.swing.GroupLayout(pnlFri);
        pnlFri.setLayout(pnlFriLayout);
        pnlFriLayout.setHorizontalGroup(
            pnlFriLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlFriLayout.createSequentialGroup()
                .addComponent(jLabel7)
                .addContainerGap())
            .addComponent(jScrollPane6, javax.swing.GroupLayout.DEFAULT_SIZE, 468, Short.MAX_VALUE)
        );
        pnlFriLayout.setVerticalGroup(
            pnlFriLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlFriLayout.createSequentialGroup()
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane6, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE))
        );

        tblSat.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane7.setViewportView(tblSat);

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel8.setText("Lørdag");

        javax.swing.GroupLayout pnlSatLayout = new javax.swing.GroupLayout(pnlSat);
        pnlSat.setLayout(pnlSatLayout);
        pnlSatLayout.setHorizontalGroup(
            pnlSatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlSatLayout.createSequentialGroup()
                .addComponent(jLabel8)
                .addContainerGap(414, Short.MAX_VALUE))
            .addComponent(jScrollPane7, javax.swing.GroupLayout.DEFAULT_SIZE, 468, Short.MAX_VALUE)
        );
        pnlSatLayout.setVerticalGroup(
            pnlSatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlSatLayout.createSequentialGroup()
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane7, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE))
        );

        tblSun.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane8.setViewportView(tblSun);

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel9.setText("Søndag");

        javax.swing.GroupLayout pnlSunLayout = new javax.swing.GroupLayout(pnlSun);
        pnlSun.setLayout(pnlSunLayout);
        pnlSunLayout.setHorizontalGroup(
            pnlSunLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlSunLayout.createSequentialGroup()
                .addComponent(jLabel9)
                .addContainerGap(409, Short.MAX_VALUE))
            .addComponent(jScrollPane8, javax.swing.GroupLayout.DEFAULT_SIZE, 468, Short.MAX_VALUE)
        );
        pnlSunLayout.setVerticalGroup(
            pnlSunLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlSunLayout.createSequentialGroup()
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane8, javax.swing.GroupLayout.DEFAULT_SIZE, 213, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout pnlActivityScheduleLayout = new javax.swing.GroupLayout(pnlActivitySchedule);
        pnlActivitySchedule.setLayout(pnlActivityScheduleLayout);
        pnlActivityScheduleLayout.setHorizontalGroup(
            pnlActivityScheduleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnlFri, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(pnlSat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(pnlSun, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        pnlActivityScheduleLayout.setVerticalGroup(
            pnlActivityScheduleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlActivityScheduleLayout.createSequentialGroup()
                .addComponent(pnlFri, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlSat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlSun, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabel1.setText("Programoversigt");

        lblActivityName.setFont(new java.awt.Font("Tahoma", 0, 18));
        lblActivityName.setText("navn");

        lblActivityTimePlace.setFont(new java.awt.Font("Tahoma", 1, 12));
        lblActivityTimePlace.setText("tid og sted");

        jScrollPane1.setBorder(null);

        txtActivityDescription.setEditable(false);
        txtActivityDescription.setFocusable(false);
        jScrollPane1.setViewportView(txtActivityDescription);

        lblActivityRequiresSignup.setText("Du skal tilmelde dig for at kunne deltage i aktiviteten");

        btnStartSignup.setText("Tilmeld");
        btnStartSignup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStartSignupActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlDetailsLayout = new javax.swing.GroupLayout(pnlDetails);
        pnlDetails.setLayout(pnlDetailsLayout);
        pnlDetailsLayout.setHorizontalGroup(
            pnlDetailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDetailsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlDetailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblActivityName)
                    .addComponent(lblActivityTimePlace)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 392, Short.MAX_VALUE)
                    .addGroup(pnlDetailsLayout.createSequentialGroup()
                        .addComponent(lblActivityRequiresSignup)
                        .addGap(18, 18, 18)
                        .addComponent(btnStartSignup)))
                .addContainerGap())
        );
        pnlDetailsLayout.setVerticalGroup(
            pnlDetailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDetailsLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblActivityName)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblActivityTimePlace)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(pnlDetailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblActivityRequiresSignup)
                    .addComponent(btnStartSignup))
                .addContainerGap(630, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(pnlActivitySchedule, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(pnlDetails, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnlDetails, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pnlActivitySchedule, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnStartSignupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartSignupActionPerformed
        SignupView frame = new SignupView();
        frame.startSignup(selectedActivity);
        frame.setVisible(true);
    }//GEN-LAST:event_btnStartSignupActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnStartSignup;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JScrollPane jScrollPane7;
    private javax.swing.JScrollPane jScrollPane8;
    private javax.swing.JLabel lblActivityName;
    private javax.swing.JLabel lblActivityRequiresSignup;
    private javax.swing.JLabel lblActivityTimePlace;
    private javax.swing.JPanel pnlActivitySchedule;
    private javax.swing.JPanel pnlDetails;
    private javax.swing.JPanel pnlFri;
    private javax.swing.JPanel pnlSat;
    private javax.swing.JPanel pnlSun;
    private javax.swing.JTable tblFri;
    private javax.swing.JTable tblSat;
    private javax.swing.JTable tblSun;
    private javax.swing.JTextPane txtActivityDescription;
    // End of variables declaration//GEN-END:variables
}
